package org.microblog.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.microblog.dao.BlogDao;
import org.microblog.dao.CollectionDao;
import org.microblog.dao.impl.BlogDaoImpl;
import org.microblog.dao.impl.CollectionDaoImpl;
import org.microblog.entity.Blog;
import org.microblog.entity.Collection;
import org.microblog.service.CollectionService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Service("collectionService")
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionDao collectionDao;

    public void setCollectionDao(CollectionDao collectionDao) {
        this.collectionDao = collectionDao;
    }

    @Override
    public int getCollectionCountByBid(int bid) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession();
        //CollectionDao dao=sqlSession.getMapper(CollectionDao.class);
        int ret=collectionDao.getCollectionCountByBid(bid);
        //sqlSession.commit();
       // sqlSession.close();
        return ret;
    }

    @Override
    public int addCollection(Collection collection) throws SQLException {
      //  Connection conn = null;
        int result;
        try {
           // conn = DatabaseUtil.getConnection();
            //conn.setAutoCommit(false);

           // CollectionDao collectionDao = new CollectionDaoImpl(conn);
            if(collectionDao.findCollection(collection.getUid(),collection.getBid())==null){
                result = collectionDao.addCollection(collection);
            }else{
                result = -1;
            }

           // conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
//            if (conn != null)
//                try {
//                    conn.rollback();
//                } catch (SQLException e1) {
//                    e1.printStackTrace();
//                }
            throw e;
        } finally {
           // DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }

    @Override
    public Collection findCollection(int uid, int bid) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession();
        //CollectionDao dao=sqlSession.getMapper(CollectionDao.class);
        //sqlSession.commit();
        //sqlSession.close();
        Collection collection=collectionDao.findCollection(uid, bid);
        return collection;
    }

    @Override
    public int deleteCollection(Collection collection) throws SQLException {
        //Connection conn = null;
        int result;
        try {
            //conn = DatabaseUtil.getConnection();
            //conn.setAutoCommit(false);

          //  CollectionDao collectionDao = new CollectionDaoImpl(conn);
            if(collectionDao.findCollection(collection.getUid(),collection.getBid())!=null){
                result = collectionDao.deleteCollection(collection);
            }else{
                result = -1;
            }

            //conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
//            if (conn != null)
//                try {
//                    conn.rollback();
//                } catch (SQLException e1) {
//                    e1.printStackTrace();
//                }
            throw e;
        } finally {
           // DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }

    @Override
    public int deleteCollectionByBid(int bid) throws SQLException {
       // SqlSession sqlSession=sessionFactory.openSession();
        //CollectionDao dao=sqlSession.getMapper(CollectionDao.class);
        //sqlSession.commit();
       // sqlSession.close();
        int ret=collectionDao.deleteCollectionByBid(bid);
        return ret;
    }

    @Override
    public void getCollectionsByPage(int uid, String keyword, Page pageObj) throws SQLException {
       // Connection conn = null;
        try{
           // conn = DatabaseUtil.getConnection();
           // CollectionDao collectionDao = new CollectionDaoImpl(conn);
            int totalCount = collectionDao.getCollectionCountByUidAndKeyword(uid,keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = collectionDao.getCollectionsByPage(uid,keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
           // DatabaseUtil.closeAll(conn,null,null);
        }
    }
}
